#!/usr/bin/env ruby
# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
# frozen_string_literal: true

require 'set'
require_relative '../defconfig.rb'

start_pod

names = Set.new %w[
  SCHED_HOST
  SCHED_PORT
  SCHED_NODES
  REDIS_HOST
  REDIS_PORT
  REDIS_PASSWD
  ETCD_USER
  ETCD_PASSWORD
  ES_HOST
  ES_PORT
  ES_USER
  ES_PASSWORD
  OS_HTTP_HOST
  OS_HTTP_PORT
  INITRD_HTTP_HOST
  INITRD_HTTP_PORT
  TASKQUEUE_PORT
  TASKQUEUE_HOST
  GIT_SERVER
  MASTER_FLUENTD_HOST
  MASTER_FLUENTD_PORT
  SSHR_PORT
  SSHR_PORT_BASE
  SSHR_PORT_LEN
  SRV_HTTP_CCI_HOST
  SRV_HTTP_CCI_PORT
  lab
  lab_id
]

defaults = relevant_defaults(names)
defaults['SSHR_PORT'] ||= 5051
defaults['SSHR_PORT_BASE'] ||= 21000
defaults['SSHR_PORT_LEN'] ||= 2000
defaults['SCHED_PORT'] ||= '3000'
defaults['MASTER_FLUENTD_PORT'] ||= '24224'
defaults['SRV_HTTP_CCI_PORT'] ||= '11307'

service_authentication = relevant_service_authentication(names)
defaults.merge!(service_authentication)

env = docker_env(defaults)

CCI_REPOS = '/c'
DEFAULT_LKP = "#{CCI_REPOS}/lkp-tests"
DEFAULT_CCI = "#{CCI_REPOS}/compass-ci"
LAB = defaults['lab'] || 'z9'
sched_nodes = defaults.delete('SCHED_NODES')
SCHED_PORT = defaults['SCHED_PORT']

sched_nodes.each do |node|
  docker_rm "s001-alpine-#{node['port']}"
end

lab_id = defaults['lab_id']
unless defaults['lab_id'].nil? or defaults['lab_id'].is_a? Integer
  puts "expect Null or Integer lab_id, got #{defaults['lab_id']}"
  exit 1
end
if lab_id and lab_id >= 1000
  puts "too large lab_id, at most 3-digit number"
  exit 1
end

sched_nodes.each do |node|
  node_host = node['host']
  node_port = node['port']
  worker_id = SCHED_PORT.to_i - 3000
  if worker_id >= 10
    puts "too large worker_id, at most 1-digit number"
    exit 1
  end
  worker_id = nil if worker_id == 0 and lab_id.nil?

  bind_list = []
  [node_host, '172.17.0.1'].uniq.each do |ip|
    bind_list << '-p'
    bind_list << "#{ip}:#{node_port}:#{node_port}"
  end

  cmd = %W[
    docker run
    --name s001-alpine-#{node_port}
    -d
    -u 1090:1090
  ] + bind_list + env + %W[
    -e CCI_REPOS=#{CCI_REPOS}
    -e LKP_SRC=#{DEFAULT_LKP}
    -e CCI_SRC=#{DEFAULT_CCI}
    -e NODE_PORT=#{node_port}
    -e WORKER_ID=#{worker_id}
    -e LAB_ID=#{defaults['lab_id']}
    -e ETCD_USER=#{defaults['ETCD_USER']}
    -e ETCD_PASSWORD=#{defaults['ETCD_PASSWORD']}
    -v #{CCI_REPOS}/lab-#{LAB}:#{CCI_REPOS}/lab-#{LAB}
    -v /srv/cci/scheduler/alpine:/srv/cci/scheduler
    -v /srv/tmp/public:/c/cci/scheduler/public
    -v /etc/compass-ci:/etc/compass-ci:ro
    -v /etc/localtime:/etc/localtime:ro
    -v /srv/result:/srv/result
    -v /srv/initrd:/srv/initrd:ro
    -v /srv/initrd/osimage/custom:/srv/initrd/osimage/custom
    -v /srv/os:/srv/os:ro
    -v /srv/upload-files:/srv/upload-files:rw
    -v /srv/cci/user-files:/srv/cci/user-files:rw
    --log-opt mode=non-blocking
    --log-opt max-buffer-size=4m
    --log-opt tag=scheduler-#{SCHED_PORT}
    sch-ruby-a:v0.00d-#{SCHED_PORT}
  ]

  cmd += ['sh', '-c', 'umask 002 && ./scheduler']

  system(*cmd)
end
